<%@ page import="ar.edu.itba.bundundu.util.EventProperties"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.google.gdata.data.calendar.CalendarEntry"%>
<%@page import="org.apache.commons.lang.*"%>

<style type="text/css">
#events tr:hover {
	background-color: #F7F7F7;
}

#events {
	border-style: solid none solid none;
	border-color: #5872A7;
}
</style>
<script>
<!--
function detalles(i)
{
        var details;
        details = document.getElementById(i);

        if(details.getStyle('visibility') == 'hidden')
        {
                details.setStyle('visibility', 'visible');
        }
        else
        {
                details.setStyle('visibility', 'hidden');
        }
}
//-->
</script>

<%
  request.setAttribute("tab", 5);
%>
<%@ include file="/header.jsp"%>

<%
	String mensaje = ""; 
	if(request.getAttribute("inserted_title") != null){
		 mensaje = " <strong>" + request.getAttribute("inserted_title")
		+ "</strong> 	is now in your google calendar." 
		+ " If you want, you can add another event.";	
		 %><fb:success><fb:message>Yupi!</fb:message> 
		  <%= mensaje %></fb:success><%
	}else if(request.getAttribute("error") != null){
	  mensaje = " <strong>" + request.getAttribute("error")
		+ "</strong><br>Please try again later.";	
		 %><fb:error><fb:message>Ups!</fb:message> 
		  <%= mensaje %></fb:error><%
	}
	
	if(request.getAttribute("success")!=null){
	  mensaje = " <strong>" + request.getAttribute("success") + "</strong>";	
		 %><fb:success><fb:message>Yupi!</fb:message> 
		  <%= mensaje %></fb:success><%
	}
%>



<%
  ArrayList<EventProperties> events;
  ArrayList<CalendarEntry> calendars;
  if(!((events = (ArrayList<EventProperties>)request.getAttribute("events")) == null ||
    (calendars=(ArrayList<CalendarEntry>)request.getAttribute("calendars"))==null)){

%>
<p>These are your Facebook events:</p>
    <table id="events" width="100%" rules="rows">
  	<th></th>
  	<th>Event</th>
  	<th>RSVP</th>
  	<th>Add to Calendar</th>
  	<%
  	int i = 0;
  	EventProperties props;
  	Iterator<EventProperties> it = events.iterator();
  	StringEscapeUtils escapador = new StringEscapeUtils(); 
  	while(it.hasNext()){
  	  props = (EventProperties)it.next();
  	%>
  	<tr>
  		<td>
  			<img src="<%= props.getPic() %>">
  		</td>
  		<td>
  			<p onclick="detalles(<%= i %>);">
  			<strong><%=props.getName() + "</strong> (click for details)</p>"%>
  			<div id="<%= i %>" style="visibility: hidden">
  				<strong>Start Date: </strong><%= props.getFecha_inicio() %> <br>
  				<strong>Start Hour: </strong><%= props.getHora_inicio() %> <br>
  				<strong>End Date: </strong><%= props.getFecha_fin() %> <br>
  				<strong>End Hour: </strong><%= props.getHora_fin() %> <br>
  				<strong>Description: </strong><%= props.getDescription() %>
  			</div>
  		</td>
  		<td>
  		<form id="rsvps" action="<%= url %>rsvp">
  			<input type="radio" name="answer" value="attending" <%= ((props.getRsvp_status()!=null && props.getRsvp_status().equals("attending"))? "checked":"") %> onClick="Facebook.showPermissionDialog('rsvp_event')">Yes<br>
			<input type="radio" name="answer" value="declined" <%= ((props.getRsvp_status()!=null && props.getRsvp_status().equals("declined"))? "checked":"") %> onClick="Facebook.showPermissionDialog('rsvp_event')">No<br>
			<input type="radio" name="answer" value="unsure" <%= ((props.getRsvp_status()!=null && props.getRsvp_status().equals("unsure"))? "checked":"") %> onClick="Facebook.showPermissionDialog('rsvp_event')">Maybe<br>
			<input type="hidden" name="eid" value="<%= props.getEid() %>">
			<input type="submit" name="botonito" value="RSVP">
  		</form>
		</td>
  		<td>
  			<form method="POST" action="<%= url %>newcalendarevent">
  			<select	name="calendar">
  				<%
  				  for (int j = 0; j < calendars.size(); j++) {
  				        CalendarEntry entry = calendars.get(j);
  				%>
  				<option value="<%= entry.getId().toString() %>">
  					<%= entry.getTitle().getPlainText() %>
  				</option>
  				<%}%>
  			</select>
	  			<input type="hidden" name="is_post" value="true"/>
	  			<input type="hidden" name="title" value="<%= escapador.escapeHtml(props.getName()) %>"/>
	  			<input type="hidden" name="desc" value="<%= escapador.escapeHtml(props.getDescription()) %>"/>
	  			<input type="hidden" name="where" value="<%= escapador.escapeHtml(props.getLocation()) %>"/>
	  			<input type="hidden" name="startDate" value="<%= props.getFecha_inicio() %>"/>
	  			<input type="hidden" name="startTime" value="<%= props.getHora_inicio() %>"/>
	  			<input type="hidden" name="endDate" value="<%= props.getFecha_fin() %>"/>
	  			<input type="hidden" name="endTime" value="<%= props.getHora_fin() %>"/>
	  			<input type="hidden" name="prox" value="events"/>
	  			<input type="submit" name="agregar<%= i%>" value="Add to calendar"/>
  			</form>
  		</td>
  	</tr>
  	<%
  		i++;
  	  }//while(it.hasnext())
  	%>
  </table>
<%
  }//else
%>
